import React, {Component} from 'react';
import {Button, Form, Input} from "antd";
import Notice from  '../../../components/Notice'
import {withRouter} from "react-router-dom";
import {userLogin} from "../../../network/user";  //ajax
import {PubSub} from 'pubsub-js'
class GoIn extends Component {
    state = {
        isLoading: false
    }
    //如果数据验证成功时进行ajax请求进行登录
    onFinish = async (values) => {
        this.setState({
            isLoading: true
        })
        let result = await userLogin(values.email,values.password);
        if(result.code === 201){
            setTimeout(() => {
                this.setState({
                    isLoading : false
                })
            },1000)
            Notice('error','登录失败','用户名或密码不正确，请重新输入')
            this.form.resetFields()
        }
        if(result.code === 200){
            const {uuid,unickname,uemail} = result.data
            sessionStorage.setItem("userId", uuid);
            sessionStorage.setItem("userNickName",unickname);
            sessionStorage.setItem("userEmail",uemail)
            setTimeout(() => {
                this.setState({
                    isLoading : false
                })
            },1000)
            Notice('success','登录成功','2秒之后将跳转至上一页')
            //发送事件！有用户登录，并传送用户的id号
            PubSub.publish('userLogin',uuid);
            setTimeout(() => {
                this.props.history.go(-1)
            },3000)
        }
    }
    render() {
        return (
            <div>
                <Form
                    ref={node => this.form = node}
                    name="basic"
                    labelCol={{
                        span: 7,
                    }}
                    wrapperCol={{
                        span: 10,
                    }}
                    initialValues={{
                        remember: true,
                    }}
                    onFinish={this.onFinish}
                    onFinishFailed={this.onFinishFailed}
                >
                    <Form.Item
                        name="email"
                        label="邮箱"
                        rules={[
                            {
                                type: 'email',
                                message: 'The input is not valid E-mail!',
                            },
                            {
                                required: true,
                                message: 'Please input your E-mail!',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        label="密码"
                        name="password"
                        rules={[
                            {
                                required: true,
                                message: 'Please input your password!',
                            },
                        ]}
                    >
                        <Input.Password />
                    </Form.Item>

                    <Form.Item
                        wrapperCol={{
                            offset: 9,
                            span: 24,
                        }}
                    >
                        <Button type="primary" htmlType="submit" loading={this.state.isLoading}>
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        );
    }
}

export default withRouter(GoIn);